<?php

namespace app\supplier\model\product;

use app\common\model\product\Recommend as RecommendModel;
use app\common\model\product\RecommendList;

/**
 * 推荐模型
 */
class Recommend extends RecommendModel
{

    /**
     * 获取列表
     */
    public function getList($param, $supplier_id = 0)
    {
        $model = new static();
        $where = $this->disposeWhere($param);
        $model = $this->assWhere($model, $where);
        $lists = $model->where('shop_supplier_id','=',$supplier_id)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($param);
        return $lists;
    }

    /**
     * 添加新记录
     */
    public function add($data, $supplier_id)
    {
        $data['app_id']      = self::$app_id;
        $data['shop_supplier_id']    = $supplier_id;
        $data['province_id'] = $data["region_id"][0] ?? 0;
        $data['city_id']     = $data["region_id"][1] ?? 0;
        $data['county_id']   = $data["region_id"][2] ?? 0;
        $data['audit_status']   =  0;
        unset($data["region_id"]);
        return $this->save($data);
    }

    /**
     * 编辑记录
     */
    public function edit($data, $supplier_id)
    {
        $data['update_time']  = time();
        $data["audit_status"] = $data['audit_status'] ?? 0;
        $data['province_id']  = $data["region_id"][0] ?? 0;
        $data['city_id']      = $data["region_id"][1] ?? 0;
        $data['county_id']    = $data["region_id"][2] ?? 0;
        unset($data["region_id"]);
        if ($supplier_id != $this["shop_supplier_id"]) {
            $this->error = "你无权修改此推荐";
            return false;
        }
        return $this->save($data);
    }

    /**
     * 删除
     */
    public function remove()
    {
        // 判断是否存在
        $Count = (new RecommendList)->where('module_id', '=', $this['id'])
            ->where('status', '=', '1')
            ->count();
        if ($Count > 0) {
            $this->error = '该推荐使用中，不允许删除';
            return false;
        }
        return $this->delete();
    }


}